/**
 * 系统初始化工作
 */
package cn.antia.cep.common;

import java.util.*;
import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.*;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.antia.cwin.SysConf;
import cn.antia.cep.db.hibernate.bean.VisitLog;

/**
 * @author sunkeei
 * 
 */
public class InitServlet extends HttpServlet {
	/**
	 * init LOG4J
	 * */
	public void init() throws ServletException {
		super.init();
		ContextBean.context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		//ContextBean.context = (ApplicationContext)this.getServletContext().getAttribute(ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX);
		if(ContextBean.context == null){
			AntiaLog.error("Cann't get application Context.Please confirm the WEB.XML is invalid");
		}
		String path = getServletContext().getRealPath("/");
		//log4j
		String log4j_config_file = path + getInitParameter("log4j_config_file");
		//system_config
		String system_config_file = path + getInitParameter("system_config_file");
		InputStream inputStream;
		Properties properties = new Properties();
		try{
			//log4j
			inputStream = new FileInputStream(log4j_config_file);
			properties.load(inputStream);
			String logFileName = properties.getProperty("log4j.appender.file.File");
			logFileName = path + "log/" + logFileName;
			properties.setProperty("log4j.appender.file.File", logFileName);
			PropertyConfigurator.configure(properties);
			AntiaLog.info("Log4j init finished..");
			// sys  init
			SysConf.init(path,system_config_file);
		}catch(Exception e){
			AntiaLog.error("InitServlet init faile");
			e.printStackTrace();
		}
		AntiaLog.info("InitServlet init finished..");
	}

	public void destroy(){
		AntiaLog.info("InitServlet:Servlet container will shut down...");
		ServletContext context = this.getServletContext();
		try{
			AntiaLog.info("InitServlet:Saving cache data......");
			saveOnlineUser(context);
			AntiaLog.info("InitServlet:Save cache data finnished......");
			AntiaLog.info("InitServlet:save user testlogs finished...");
		}catch(Exception e){
			e.printStackTrace();
			AntiaLog.error("InitServlet save server data fail:" + e.getMessage());
		}
		super.destroy();
	}
	/**
	 * 保存在线用户
	 * @param context
	 * @throws AntiaException
	 */
	private void saveOnlineUser(ServletContext context)throws AntiaException{
		AntiaLog.info("InitServlet:saving online users..");
		//找到在线用户信息�
		HashMap usermap = (HashMap)context.getAttribute("usermap");
		if(usermap == null){
			AntiaLog.error("Where server will down. Online user is null.");
			return;
		}
		Iterator iterator = usermap.keySet().iterator();
		Object sessionId;
		VisitLog log;
		Calendar cs = Calendar.getInstance();
		int i = 0;
		while(iterator.hasNext()){
			sessionId = iterator.next();
			log = (VisitLog)usermap.get(sessionId);
			cs.setTime(log.getLoginTime());
			log.setLogoutTime(new Date());
			long active =  log.getLogoutTime().getTime() - cs.getTimeInMillis();
			Calendar ac = Calendar.getInstance();
			ac.setTimeInMillis(active);
			String activeDesc = ac.get(Calendar.HOUR) - 8 + " 时 " + ac.get(Calendar.MINUTE) + " 分 ��" + ac.get(Calendar.SECOND) + " 秒�";
			log.setActiveTime(active + "");
			log.setActiveTimeDesc(activeDesc);
			log.setComment("SESSION 销毁");
			//service.addEntity(log);
			++i ;
		}
		AntiaLog.info("InitServlet:add " + i + " online user data");
		AntiaLog.info("InitServlet:save online users finished...");
	}
	
}